<?php

namespace api\controller;

// +----------------------------------------------------------------------
// | 数据结构数据控制层
// +----------------------------------------------------------------------

use common\controller\VerifyController;
use common\exception\LogicException;
use common\service\SchemaService;
use think\Request;

class SchemaController extends VerifyController
{
    protected $schemaService;

    public function __construct()
    {
        parent::__construct();
        $this->schemaService = SchemaService::getInstance();
    }

    /**
     * 创建数据结构
     * @param Request $request
     * @return \think\Response
     */
    public function create(Request $request)
    {
        $param = $request->param();
        $resData = $this->schemaService->addSchema($param["data"]);
        return json(success_response('Data structure created successfully.', $resData));
    }


    /**
     * 更新数据结构
     * @param Request $request
     * @return \think\Response
     */
    public function update(Request $request)
    {
        $param = $request->param();
        $resData = $this->schemaService->modifySchema($param['data']);
        return json(success_response('Schema update succeeded.', $resData));
    }

    /**
     * 删除数据结构
     * @param Request $request
     * @return \think\Response
     */
    public function delete(Request $request)
    {
        try {
            $param = $request->param();
            $resData = $this->schemaService->deleteSchema($param["param"]["filter"]);
            return json(success_response('Data structure deleted successfully.', $resData));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }


    /**
     * 给数据结构添加模块
     * @param Request $request
     * @return \think\Response
     * @throws \Exception
     */
    public function saveModuleRelation(Request $request)
    {
        try {
            $resData = $this->schemaService->saveModuleRelation($request->param());
            return json(success_response('Successfully added data structure module relationship.', $resData));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 没有坐标创建数据结构
     * @param Request $request
     * @return \think\Response
     * @throws \Exception
     */
    public function createWithoutPos(Request $request)
    {
        try {
            $param = $request->param();
            $resData = $this->schemaService->saveSchemaModuleRelation($param["data"]);
            return json(success_response('Data structure created successfully.', $resData));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 获取指定数据结构关联模型
     * @param Request $request
     * @return \think\Response
     */
    public function getSchemaModuleRelationData(Request $request)
    {
        $param = $request->param();
        $resData = $this->schemaService->getSchemaModuleRelationData($param["param"]["filter"]["schema_id"]);
        return json(success_response('Data query succeeded.', $resData));

    }
}
